//
//  FlutterHandler.swift
//  flutter_video_info
//
//  Created by Landon on 2025/1/10.
//

import Foundation

class VideoHandler: NSObject, FlutterStreamHandler {
    
    private var _eventSink: FlutterEventSink?
    
    func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
        _eventSink = events;
        return nil;
    }
    
    func onCancel(withArguments arguments: Any?) -> FlutterError? {
        _eventSink = nil;
        return nil;
    }
    
    public func updateProgress(_ progress: Float) {
        if (_eventSink != nil) {
            _eventSink!(progress)
        }
    }
}

private let videoHandler = VideoHandler()

class FlutterHandler {
    
    private let videoCompressChannel: FlutterEventChannel
    
    private let eventChannelName = "flutter_video_info_stream"
    
    init(binding: FlutterPluginRegistrar) {
        videoCompressChannel = FlutterEventChannel(
            name: eventChannelName,
            binaryMessenger: binding.messenger());
        
        videoCompressChannel.setStreamHandler(videoHandler)
    }
    
    public func dispose() {
        videoCompressChannel.setStreamHandler(nil)
    }
    
    public static func updateCompressProgress(_ progress: Float) {
        videoHandler.updateProgress(progress)
    }
}

